package com.ruoyi.jl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.jl.domain.GameProfit;
import com.ruoyi.jl.domain.Player;
import com.ruoyi.jl.mapper.GameProfitMapper;
import com.ruoyi.jl.mapper.PlayerMapper;
import com.ruoyi.jl.service.IGameProfitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 游戏税收记录 服务层实现
 * 
 * @author ruoyi
 * @date 2019-06-01
 */
@Slf4j
@Service
public class GameProfitServiceImpl extends ServiceImpl<GameProfitMapper, GameProfit> implements IGameProfitService {

    @Autowired
    PlayerMapper playerMapper;


    @Override
    public List<GameProfit> list(GameProfit gameProfit){
        LambdaQueryWrapper<GameProfit> wrapper = new LambdaQueryWrapper<>();
        if(gameProfit != null){
            if(StringUtils.isNotEmpty(gameProfit.getId())){
                wrapper.eq(GameProfit::getId, gameProfit.getId());
            }
            if(StringUtils.isNotEmpty(gameProfit.getGameId())){
                wrapper.eq(GameProfit::getGameId, gameProfit.getGameId());
            }
            if(StringUtils.isNotEmpty(gameProfit.getGold())){
                wrapper.eq(GameProfit::getGold, gameProfit.getGold());
            }
            if(StringUtils.isNotEmpty(gameProfit.getCreateTime())){
                wrapper.eq(GameProfit::getCreateTime, gameProfit.getCreateTime());
            }
        }
        return baseMapper.selectList(wrapper);
    }

    /**
     * 存取银行金币
     * @param playerId
     * @param accessGold
     * @param type
     * @param safePwd
     */
    @Override
    @Transactional
    public void accessBankGold(Integer playerId, Integer accessGold, Integer type, String safePwd) throws CustomException {
        // 判断密码,库存是否充足
        Player player = playerMapper.selectById(playerId);
        String storeSafePwd = player.getSafePwd();// 银行密码
        Integer safeGold = player.getSafeGold();// 银行金币
        Integer carryGold = player.getCarryGold();// 携带金币

        if(!safePwd.equals(storeSafePwd)) {// 密码错误
            throw CustomException.instance(1, "密码错误");
        }
        if(type == 0) {// 存金币
            if(carryGold < accessGold) {// 携带金币不足
                throw CustomException.instance(2, "携带金币不足");
            }
            carryGold -= accessGold;
            safeGold += accessGold;
        } else if(type == 1) {// 取金币
            if(safeGold < accessGold) {// 银行金币不足
                throw CustomException.instance(3, "银行金币不足");
            }
            safeGold -= accessGold;
            carryGold += accessGold;
        }
        player = new Player();
        player.setPlayerId(playerId);
        player.setCarryGold(carryGold);
        player.setSafeGold(safeGold);
        playerMapper.updateById(player);
    }
}
